package com.mrd.gtimpframe.action;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.gtimpframe.entity.GoodsCode;
import com.mrd.gtimpframe.entity.GoodsCodeKey;
import com.mrd.gtimpframe.service.GoodsCodeService;
import com.mrd.util.FormBeanUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Created by dusy on 2017/7/26.
 */
@Controller
@RequestMapping("/goods")
public class GoodsCodeController {
    @Autowired
    private GoodsCodeService goodsCodeService;

    @RequestMapping("/getGoodsCodeList")
    @ResponseBody
    public Page<Map<String, Object>> getGoodsCode(Jqgrid jqgrid) {
        GoodsCode params = FormBeanUtil.getEntityFromJqgrid(jqgrid, GoodsCode.class);

        Pageable page = jqgrid.getPage();
        PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        Sort.Order order = jqgrid.getOrder();
        if (order != null) {
            String direction = order.getDirection().toString();
            String propertyName = order.getProperty();
            PageHelper.orderBy(propertyName + " " + direction);
        }
        List<GoodsCode> goodsCodeList = goodsCodeService.selectSelective(params);
        PageInfo<GoodsCode> pi = new PageInfo<>(goodsCodeList);

        return new PageImpl(goodsCodeList, page, pi.getTotal());
    }

    @RequestMapping("/main")
    public String goodsCode() {
        return "gtimpframe/goodsCode/goodsCodeMain";
    }

    @RequestMapping("/edit")
    public String goodsCodeEdit(GoodsCodeKey goodsCodeKey, Model model) {
        model.addAttribute("goodsCode", goodsCodeService.selectByPrimaryKey(goodsCodeKey));
        return "/gtimpframe/goodsCode/goodsCodeEdit";
    }

    @RequestMapping("/saveGoodsCode")
    @ResponseBody
    public Result addGoodsCode(GoodsCode goodsCode) {
        if (goodsCode == null) {
            return Result.newFailInstance("保存信息接收失败");
        }
        if (goodsCode.getCodeT() == null || goodsCode.getCodeT().length() != 8) {
            return Result.newFailInstance("商品编码必须为8位");
        }
        if (goodsCode.getCodeS() == null || goodsCode.getCodeS().length() != 2) {
            return Result.newFailInstance("附加码必须为2位");
        }
        if (goodsCode.getUnit1() == null) {
            return Result.newFailInstance("必须填写法定单位");
        }
        if (goodsCodeService.saveOrUpdate(goodsCode) > 0) {
            return Result.newSuccessInstance("保存成功");
        }
        return Result.newFailInstance("保存失败");
    }

    /**
     * 删除原有的数据
     * @param codeT
     * @param codeS
     * @return
     */
    @RequestMapping("/deleteGoods")
    @ResponseBody
    public boolean deleteGoods(String codeT,String codeS) {
        if (codeT!=null&&codeS!=null){
            GoodsCodeKey codeKey=new GoodsCodeKey();
            codeKey.setCodeT(codeT);
            codeKey.setCodeS(codeS);
            if (goodsCodeService.deleteByPrimaryKey(codeKey)>0)
                return true;
        }
        return  false;
    }
}
